/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Service;

import DAO.CargoDAO;
import DAO.DAOFactory;
import DAO.exceptions.NonexistentEntityException;
import DAO.exceptions.RollbackFailureException;
import Entidad.Cargo;
import VO.CargoVO;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;

/**
 *
 * @author xSebastiaNx
 */
public class ResponsabilityServices {
    public void createCargo(CargoVO cargo, EntityManager em) throws RollbackFailureException, Exception{
        CargoDAO dao = (CargoDAO)DAOFactory.createDAO(DAOFactory.CARGO);
        Cargo cargoEntity = cargo.toEntity();
        cargoEntity.setIdCargos(dao.getCargoCount(em) + 1);
        dao.create(cargoEntity, em);
    }
    
    public CargoVO readCargo(int id, EntityManager em){
        CargoDAO dao = (CargoDAO)DAOFactory.createDAO(DAOFactory.CARGO);
        CargoVO cargo = dao.findCargo(id, em).toVO();
        return cargo;
    }
    
    
    
    public List<CargoVO> getAllResponsabilities(EntityManager em){
        CargoDAO dao = (CargoDAO)DAOFactory.createDAO(DAOFactory.CARGO);
        List<CargoVO> listaVo = new ArrayList<CargoVO>();
        List<Cargo> listaCargos = dao.findCargoEntities(em);
        Iterator listaIterator = listaCargos.listIterator();
        while(listaIterator.hasNext()){
            Cargo cargo = (Cargo)listaIterator.next();
            listaVo.add(cargo.toVO());
        }
        return listaVo;
    }

    public void updateResponsability(Cargo cargo, EntityManager em) throws NonexistentEntityException {
        try {
            CargoDAO dao = (CargoDAO)DAOFactory.createDAO(DAOFactory.CARGO);      
            dao.edit(cargo, em);
        } catch (RollbackFailureException ex) {
            Logger.getLogger(ResponsabilityServices.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(ResponsabilityServices.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
